package com.pearl.sharding.level.table.demo.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearl.sharding.level.table.demo.entity.OrderEntity;
import com.pearl.sharding.level.table.demo.service.OrderService;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author 自动代码生成
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @GetMapping("/list")
    public List<OrderEntity> list() {
        return orderService.list();
    }


    @GetMapping("/update")
    public Object update() {
        // 更新:ds_order_0.t_order_0  1760247736146894850
        OrderEntity order = orderService.getById(1760247736146894850L);
        order.setOrderTime(LocalDateTime.now());
        orderService.updateById(order);
        return "操作成功";
    }

    @GetMapping("/delete")
    public Object delete() {
        return orderService.removeById(1759507405818310658L);
    }

    @GetMapping("/page")
    public Object page() {
        return orderService.page(new Page<>(1, 10));
    }

    @GetMapping("/save")
    public Object save() {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setPhone(18888888888L);
        orderEntity.setAddress("湖南长沙");
        orderEntity.setOrderTime(LocalDateTime.now());
        orderEntity.setProductId(1L);
        orderService.save(orderEntity);
        return "操作成功";
    }

    @GetMapping("/complex")
    public Object complex() {
        // type = 1
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setType(1);
        orderEntity.setPhone(18888888888L);
        orderEntity.setAddress("湖南长沙");
        orderEntity.setOrderTime(LocalDateTime.now());
        orderEntity.setProductId(1L);
        orderService.save(orderEntity);
        // type = 2
        orderEntity.setId(null);
        orderEntity.setType(2);
        orderService.save(orderEntity);
        return "操作成功";
    }

    @GetMapping("/saveByDate")
    public Object saveByDate(String date) { // "2024-02-01 00:00:00"
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setPhone(18888888888L);
        orderEntity.setAddress("湖南长沙");
        orderEntity.setOrderTime(LocalDateTimeUtil.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        orderEntity.setProductId(1L);
        orderService.save(orderEntity);
        return "操作成功";
    }

    @GetMapping("/hint")
    public Object hit() {
        try (HintManager hintManager = HintManager.getInstance()) {
            //hintManager.addDatabaseShardingValue("ds_order", 1); // 添加数据源分片键值

            // 1. 插入到 t_order_02 表
            hintManager.addTableShardingValue("t_order", 2); // 添加表分片键值
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setPhone(18888888888L);
            orderEntity.setAddress("湖南长沙");
            orderEntity.setProductId(1L);
            orderService.save(orderEntity);
            orderEntity.setOrderTime(LocalDateTime.now());

            // 2. 只在t_order_02 表中查询
            orderService.getById(1759884860223672322L);
        }
        return "操作成功";
    }

    @GetMapping("/getById")
    public Object getById(@RequestParam Long id) {
        return orderService.getById(id);
    }

    @GetMapping("/getByRange")
    public Object getByRange() {
        // ID 集合查询
        LambdaQueryWrapper<OrderEntity> queryWrapper = Wrappers.lambdaQuery();
        orderService.listByIds(ListUtil.toList(11l));
        // 时间范围查询
        queryWrapper.clear();
        queryWrapper.between(OrderEntity::getOrderTime, "2024-02-19 00:00:00", "2024-02-20 00:00:00");
        orderService.list(queryWrapper);
        return "查询成功";
    }
}